package heima.lm.oschina.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import java.util.List;

import heima.lm.oschina.BaseApplication;
import heima.lm.oschina.interf.OnRecyclerViewItemClickListener;

/**
 * Created by herenx0ue on 2017/4/5.
 */

public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter {

    protected List<T> mList;
    protected OnRecyclerViewItemClickListener mItemClickListener;

    protected BaseRecyclerViewAdapter(List<T> list) {
        this.mList = list;
    }

    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        else
            return 0;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.mItemClickListener = onRecyclerViewItemClickListener;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (!holder.itemView.hasOnClickListeners()) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null)
                        mItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
                }
            });
        }
    }

    //添加数据下拉刷新
    public void addRefreshItem(List<T> newDatas) {

        newDatas.addAll(mList);
        mList.removeAll(mList);
        mList.addAll(newDatas);
        notifyDataSetChanged();
    }

    //下拉加载
    public void addLoadItem(List<T> newDatas) {
        mList.addAll(newDatas);
        notifyDataSetChanged();
    }

    public List<T> getDatas() {
        return mList;
    }
}
